package cn.wolfcode.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.PermissionAnnotation;
import cn.wolfcode.util.StatusEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IBusinessService service;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @PermissionAnnotation(name="预约页面",expression="appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo){
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("business",service.listAll());
        model.addAttribute("PageInfo", pageInfo);
        model.addAttribute("system",systemDictionaryItemService.getAll());
        model.addAttribute("appo", StatusEnum.values());
        return "appointment/list";
    }


    @PermissionAnnotation(name="预约删除",expression="appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    @PermissionAnnotation(name="预约新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {
        if (appointment.getId() == null) {
            String date = DateUtil.format(new Date(), "yyyyMMddHHmmss");
            String s = RandomUtil.randomNumbers(5);
            appointment.setAno(date+s);
            appointment.setCreateTime(new Date());
            appointment.setStatus(StatusEnum.PERFORM.getValue());
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }

    @RequestMapping("/updateStatus")
    public String updateStatus(Long id,Integer status){
        appointmentService.updateStatus(id,status);
        return "redirect:/appointment/list";
    }
}
